package org.nlp.algo.spellcheck;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

import com.aliasi.spell.CompiledSpellChecker;
import com.aliasi.spell.SpellChecker;
import com.aliasi.util.Streams;

public class testSpellcheck {

    private static CompiledSpellChecker readModel(File file)
            throws ClassNotFoundException, IOException {

            // create object input stream from file
            FileInputStream fileIn = new FileInputStream(file);
            BufferedInputStream bufIn = new BufferedInputStream(fileIn);
            ObjectInputStream objIn = new ObjectInputStream(bufIn);

            // read the spell checker
            CompiledSpellChecker sc = (CompiledSpellChecker) objIn.readObject();

            // close the resources and return result
            Streams.closeQuietly(objIn);
            Streams.closeQuietly(bufIn);
            Streams.closeQuietly(fileIn);
            return sc;
        }

	static void testModelLoop(SpellChecker sc, String Query) {
		
		// compute alternative spelling		
		String bestAlternative = sc.didYouMean(Query);
		
		System.out.println("best alternate " + bestAlternative);
	}

	public static void main(String[] args) throws ClassNotFoundException, IOException {
		File MODEL_FILE = new File("SpellModel");
		
		
		String Query = "高级研xiuban";
		System.out.println("Reading model from file=" + MODEL_FILE);
		CompiledSpellChecker compiledSC = readModel(MODEL_FILE);
		testModelLoop(compiledSC, Query);
	}
}
